#include <REGX52.H>
#include "Delay.h"
#include "LCD1602.h"


char MatrixKey()
{
		char Keynum = 0;//返回0很很重要，否则会返回其他值
	
		P1 = 0xff;
		P1_7= 0;
		if(P1_3 == 0){Delay(20);while(P1_3 == 0);Delay(20);Keynum = 1;}
		if(P1_2 == 0){Delay(20);while(P1_2 == 0);Delay(20);Keynum = 2;}
		if(P1_1 == 0){Delay(20);while(P1_1 == 0);Delay(20);Keynum = 3;}
		if(P1_0 == 0){Delay(20);while(P1_0 == 0);Delay(20);Keynum = 4;}
		
		P1 = 0xff;
		P1_6 = 0;
		if(P1_3 == 0){Delay(20);while(P1_3 == 0);Delay(20);Keynum = 5;}
		if(P1_2 == 0){Delay(20);while(P1_2 == 0);Delay(20);Keynum = 6;}
		if(P1_1 == 0){Delay(20);while(P1_1 == 0);Delay(20);Keynum = 7;}
		if(P1_0 == 0){Delay(20);while(P1_0 == 0);Delay(20);Keynum = 8;}
		
		P1 = 0xff;
		P1_5 = 0;
		if(P1_3 == 0){Delay(20);while(P1_3 == 0);Delay(20);Keynum = 9;}
		if(P1_2 == 0){Delay(20);while(P1_2 == 0);Delay(20);Keynum = 10;}
		if(P1_1 == 0){Delay(20);while(P1_1 == 0);Delay(20);Keynum = 11;}
		if(P1_0 == 0){Delay(20);while(P1_0 == 0);Delay(20);Keynum = 12;}
		
		P1 = 0xff;
		P1_4 = 0;
		if(P1_3 == 0){Delay(20);while(P1_3 == 0);Delay(20);Keynum = 13;}
		if(P1_2 == 0){Delay(20);while(P1_2 == 0);Delay(20);Keynum = 14;}
		if(P1_1 == 0){Delay(20);while(P1_1 == 0);Delay(20);Keynum = 15;}
		if(P1_0 == 0){Delay(20);while(P1_0 == 0);Delay(20);Keynum = 16;}
		
		return Keynum;
}